package org.footito.android.ui.activity;

import org.footito.android.R;
import org.footito.android.data.Status;
import org.footito.android.tools.VersionAndScreenSize;
import org.footito.android.ui.activity.TimelineFragment.OnStatusClickedListener;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Window;

public class TimelineActivity extends SherlockFragmentActivity implements OnStatusClickedListener {
	public static boolean isRunning = false;
	private TimelineFragment fragmentTimeline;
	private StatusInfoFragment fragmentStatusInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

		setContentView(R.layout.activity_timeline);

		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

		fragmentTimeline = (TimelineFragment) fragmentManager.findFragmentByTag(TimelineFragment.class.getSimpleName());
		if (fragmentTimeline == null) {
			fragmentTimeline = new TimelineFragment();
			fragmentTransaction.add(R.id.timeline_timelinefragmentcontainer, fragmentTimeline,
					TimelineFragment.class.getSimpleName());
		}
		fragmentTimeline.setOnStatusClickedListener(this);

		if (VersionAndScreenSize.isLargeOrUpper(this)) {
			fragmentStatusInfo = (StatusInfoFragment) fragmentManager.findFragmentByTag(StatusInfoFragment.class
					.getSimpleName());
			if (fragmentStatusInfo == null) {
				fragmentStatusInfo = new StatusInfoFragment();
				fragmentTransaction.add(R.id.timeline_statusfragmentcontainer, fragmentStatusInfo,
						StatusInfoFragment.class.getSimpleName());
			}
		}

		fragmentTransaction.commit();

		setSupportProgressBarIndeterminateVisibility(false);
	}

	@Override
	protected void onResume() {
		isRunning = true;
		super.onResume();
	}

	@Override
	protected void onPause() {
		isRunning = false;
		super.onPause();
	}

	@Override
	public void onStatusClicked(Status status) {
		if (VersionAndScreenSize.isLargeOrUpper(this)) {
			fragmentStatusInfo.setStatus(status);
		} else {
			StatusInfoActivity.launchActivity(this, status);
		}
	}
}
